//YOUB OUBODIB
#include "login.h"
#include "ui_login.h"
#include <QSqlQuery>
#include <QtSql>
#include <QtDebug>
#include <QString>
#include <QMessageBox>

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    m_db=QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("localhost");
    m_db.setDatabaseName("gbib");
    m_db.setUserName("root");
    m_db.setPassword("");

    m_db.open();

    if(m_db.open())
        {
            qDebug()<< "opened" ;
           // db.close();
        }
        else
        {
            qDebug() << "ERROR" << m_db.lastError().text();
        }

}





Login::~Login()
{
    delete ui;
}



void Login::on_btnlogin_clicked(){



    //verification login

          //recuper les champs saisies
            username = ui->txtuser->text();
            pass = ui->txtpassword->text();

          QSqlQuery sql(m_db);

                 sql.exec( QString( "SELECT utilisateur, passe from  cpt" ) );

                 //recupere les champs d base done

                 /*userbd=sql.value(0).toString
                 passbd.value(1).toString*/

                // userbd="ini", passbd="fre";

                 //just pour verifier sql

                    //ui->textEdit_Log->append(passbd);
                    //ui->textEdit_Log2->append(userbd);

                 i=0;
                 //if ( sql.next() )

          while ( sql.next())
          {


         if (sql.value(1).toString()==pass && sql.value(0).toString()==username)
            {
                QMessageBox::critical(this, tr("connected"), tr("connected."));
i++;
            }
          }

          if(i==0) QMessageBox::critical(this, tr("co connected"), tr("login error."));

}
